package com.numberone.master.modules.oa.web;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.modules.oa.entity.Delayed;
import com.numberone.master.modules.oa.entity.WorkOrder;
import com.numberone.master.modules.oa.service.DelayedService;
import com.numberone.master.modules.oa.service.WorkOrderService;
import com.numberone.master.modules.sys.entity.User;
import com.numberone.master.modules.sys.utils.UserUtils;


@Controller
@RequestMapping(value = "${adminPath}/oa/efficiency")
public class EfficiencyController extends BaseController{
	
	@Autowired
	private DelayedService delayedService;
	
	@Autowired
	private WorkOrderService workOrderService;
	
	@ModelAttribute
	public Delayed get(@RequestParam(required=false) String id) {
		
		
		Delayed entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = delayedService.get(id);
		}
		if (entity == null){
			entity = new Delayed();
		}
		return entity;
	}
	
	@RequestMapping(value = "delayedList")
	public String delayedList(HttpServletRequest request, HttpServletResponse response, Model model) {
		User currentUser = UserUtils.getUser();
		Delayed delayed = new Delayed();
		delayed.setDispose(currentUser.getId());
		Page<Delayed> page = delayedService.findList(new Page<Delayed>(request, response),delayed);
		model.addAttribute("page", page);
		return "modules/oa/delayedList";
	}
	
	@RequestMapping(value = "form")
	public String form(Delayed delayed, Model model) {
		model.addAttribute("delayed", delayed);
		return "modules/oa/delayedForm";
	}
	
	@RequestMapping(value = "save")
	public String save(Delayed delayed, Model model) {
		model.addAttribute("delayed", delayed);
		WorkOrder workOrder = workOrderService.get(delayed.getWorkOrderId());
		Date date = workOrder.getProcessingTime();
		int hour = Integer.parseInt(delayed.getExtendDate());
		Calendar cal = Calendar.getInstance();   
        cal.setTime(date); 
        cal.add(Calendar.HOUR, hour);// 24小时制   
        date = cal.getTime();  
		workOrder.setProcessingTime(date);
		delayed.setState("2");
		workOrderService.update(workOrder);
		delayedService.update(delayed);
		return "redirect:" + adminPath + "/oa/efficiency/delayedList";
	}

}
